﻿using System;
using System.Windows.Forms;
using Microsoft.Win32;

namespace CSF.WinApp.JobNotifier
{
    public partial class Login : Form
    {
        ServiceLogin.SecurityServiceClient loginBll = new ServiceLogin.SecurityServiceClient();

        public Login()
        {
            InitializeComponent();
        }

        #region Login_Login
        /// <summary>
        /// Login_Login
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Login_Load(object sender, EventArgs e)
        {
            //从注册表里读取用户名密码,进行校验,成功则直接跳转到主页面,失败,回到登录页面
            //string[] user = (new Common()).GetRegistData();
            string[] user = (new Common()).GetUserInfo();
            //如果用户名或密码为空直接返回
            if (string.IsNullOrEmpty(user[0]) || string.IsNullOrEmpty(user[1]))
            {
                return;
            }
            string ticket = loginBll.Login(user[0], user[1]);
            //校验通过
            if (ticket != null)
            {
                AppDomain.CurrentDomain.SetData("ticket", ticket);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            //开机启动
            (new Common()).StartWithWindows();
        }
        #endregion

        #region btnOK
        /// <summary>
        /// btnOK
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            //用户登录
            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                txtUserName.Focus();
                MessageBox.Show("User name is required!", "Tip", MessageBoxButtons.OK);
                return;
            }
            if (string.IsNullOrEmpty(txtUserPwd.Text))
            {
                txtUserName.Focus();
                MessageBox.Show("User password is required!", "Tip", MessageBoxButtons.OK);
                return;
            }
            string ticket=loginBll.Login(txtUserName.Text.Trim(),txtUserPwd.Text.Trim());
            if (ticket != null)
            {
                AppDomain.CurrentDomain.SetData("ticket", ticket);
                //(new Common()).SetRegistData(txtUserName.Text, txtUserPwd.Text);
                (new Common()).SetUserInfo(txtUserName.Text, txtUserPwd.Text);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("User name or password is invalid!", "Tip", MessageBoxButtons.OK);
                return;
            }
        }
        #endregion

        #region btnCancel
        /// <summary>
        /// 取消事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtUserName.Text = "";
            txtUserPwd.Text = "";
        }
        #endregion
    }
}
